/**
 * @模块名：demo
 * @包名：com.example.xml
 * @描述：XmlUtil.java
 * @版本：1.0
 * @创建人：李凯
 * @创建时间：2020年12月28日下午6:52:09
 */
package com.example.xml;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.example.utils.JSONUtil;

/**
 * @模块名：demo
 * @包名：com.example.xml
 * @类名称：XmlUtil
 * @类描述：
 * @版本：1.0
 * @创建人：李凯
 * @创建时间：2020年12月28日下午6:52:09
 */
public class XmlUtilTest {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		String path = XmlUtilTest.class.getResource("").getPath() + "/1.txt";
		String xmlStr = readTxtFile(path);
		Map<String, Object> map = parseXml(xmlStr);
		Map<String, Object> envelope = (Map<String, Object>) map.get("Envelope");
		Map<String, Object> body = (Map<String, Object>) envelope.get("Body");
		Map<String, Object> resp = (Map<String, Object>) body.get("resp");
		Map<String, Object> appBody = (Map<String, Object>) resp.get("appBody");
		if (appBody.containsKey("LIST")) {
			Object obj = appBody.get("LIST");
			if (obj instanceof List) {
				System.out.println("list");
				List<Object> list = (List<Object>) obj;
				System.out.println(JSONUtil.objToJsonStr(list));
				if (list != null && list.size() > 0) {
					System.out.println("===============");
				} else {
					System.out.println("---------------");
				}
			} else if (obj instanceof String) {
				System.out.println("String:" + obj.toString());
			} else {
				System.out.println("other");
			}
		} else {
			System.out.println("不包含list");
		}
	}

	public static Map<String, Object> parseXml(String xml) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes()));
			Element root = document.getRootElement();
			elementTomap(root, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> elementTomap(Element outele, Map<String, Object> outmap) {
		List<Element> list = outele.elements();
		int size = list.size();
		if (size == 0) {
			outmap.put(outele.getName(), outele.getTextTrim());
		} else {
			Map<String, Object> innermap = new HashMap<String, Object>();
			int i = 1;
			for (Element ele1 : list) {
				String eleName = ele1.getName();
				String value = ele1.getText();
				Object obj = innermap.get(eleName);
				if (obj == null) {
					elementTomap(ele1, innermap);
				} else {
					if (obj instanceof java.util.Map) {
						List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
						list1.add((Map<String, Object>) innermap.remove(eleName));
						elementTomap(ele1, innermap);
						list1.add((Map<String, Object>) innermap.remove(eleName));
						innermap.put(eleName, list1);
					} else if (obj instanceof String) {
						innermap.put(eleName + i, value);
						i++;
					} else {
						elementTomap(ele1, innermap);
						Map<String, Object> listValue = (Map<String, Object>) innermap.get(eleName);
						((List<Map<String, Object>>) obj).add(listValue);
						innermap.put(eleName, obj);
					}
				}
			}
			outmap.put(outele.getName(), innermap);
		}
		return outmap;
	}

	public static String readTxtFile(String filePath) {
		String result = "";
		try {
			String encoding = "GBK";
			File file = new File(filePath);
			if (file.isFile() && file.exists()) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					result += lineTxt;
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return result;
	}
}
